package com.wavehi.interceptior;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;

import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @Description
 * @Author WaveHi
 * @Date 30/11/2021 下午 9:33
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */

@Intercepts({
        @Signature(
                type = StatementHandler.class,
                method = "parameterize",
                args = java.sql.Statement.class)
})
public class MyFirstPlugin implements Interceptor {

    /*
    拦截目标方法的执行
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //获取拦截的目标方法
        Method method = invocation.getMethod();
        System.out.println("Intercept:" + method.getName());
        //执行目标方法
        Object proceed = invocation.proceed();
        return proceed;
    }

    /*
    将目标对象封装为代理对象
     */
    @Override
    public Object plugin(Object target) {
        System.out.println("Plugin : " + target);
        //封装对象
        Object wrap = Plugin.wrap(target, this);
        return wrap;
    }

    /*
    设置插件的属性
     */
    @Override
    public void setProperties(Properties properties) {

        System.out.println("插件的信息；" + properties);
    }
}
